﻿using IntoCommon.Protocol.Enum;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;

namespace AirconditionManage.IntoCommon
{
    public class UDSService
    {
        public static Dictionary<string, string> SID_Dic => new Dictionary<string, string>();

        private UDSService()
        {

        }

        private void Init()
        {
            SID_Dic.Add("10", "50");
            SID_Dic.Add("11", "51");
            SID_Dic.Add("27", "60");
            SID_Dic.Add("28", "68");
            SID_Dic.Add("3E", "7E");
            SID_Dic.Add("83", "C3");
            SID_Dic.Add("85", "C5");
            SID_Dic.Add("86", "C6");
            SID_Dic.Add("87", "C7");
            SID_Dic.Add("2C", "6C");
            SID_Dic.Add("19", "59");
            SID_Dic.Add("31", "71");
        }

        /// <summary>
        /// UDS请求应答校验
        /// </summary>
        /// <returns></returns>
        public static bool UDSCheck(byte[] request, byte[] response)
        {
            if (response == null || response.Length < 2) return false;
            // 应答服务ID（response[1]）应与请求中的服务ID相同
            if (response[1] != request[1]) return false;

            // 应答长度（response[0] & 0x0FF）应与应答数据长度相符
            if (response.Length - 2 != (response[0] & 0x0FF)) return false;

            // 应答场（response[2]）应表示负应答（0x40-0x7F）或正常应答（0x20-0x3F）
            if (!(response[2] >= 0x40 && response[2] <= 0x7F) && !(response[2] >= 0x20 && response[2] <= 0x3F))
            {
                return false;
            }

            // 请求ID（response[3]）和请求的应答ID（response[4]）应匹配
            if (response[3] != request[4])
            {
                return false;
            }

            return true;
        }

        /// <summary>
        /// 诊断会话控制
        /// </summary>
        public static bool DiagnosticSessionControl(byte[]request, byte[] response)
        {
            return true;
        }

        /// <summary>
        /// 安全访问
        /// </summary>
        /// <param name="request"></param>
        /// <param name="response"></param>
        /// <returns></returns>
        public static bool SecurityAccess(byte[] request, byte[] response)
        {
            return true;
        }
        /// <summary>
        /// 待机握手
        /// </summary>
        /// <param name="request"></param>
        /// <param name="response"></param>
        /// <returns></returns>
        public static bool TesetPresent(byte[] request, byte[] response)
        {
            return true;
        }
        /// <summary>
        /// 通过ID读数据
        /// </summary>
        /// <param name="request"></param>
        /// <param name="response"></param>
        /// <returns></returns>
        public static bool ReadDataByIdentifier(byte[] request, byte[] response)
        {
            return true;
        }
    }
}
